<?php

namespace app\api\controller;

use app\admin\model\cases\Ctype;
use app\api\validate\PatrolTaskValidate;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 问题清单管理
 */
class PatrolTask extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\PatrolTask();
        $this->modelUser = new \app\admin\model\userstaff\User();
        $this->modelType = new Ctype();
        $this->modelShop = new \app\admin\model\shop\User();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     *  列表
     * @return void
     */
    public function  list()
    {
        $input = input();
        //根据不同的人显示不同的数据
        $userinfo = $this->auth->getUserinfo();
        try {
            //状态:1=巡查人员,2=队长,3=支队长
            $whereOR = [];
            switch ($userinfo['type']) {
                case 1:
                    $where['user_staff_id'] = $userinfo['staff_id'];
                    break;
                case 2:
                    $where['captain_id'] = $userinfo['staff_id'];
                    $whereOR['user_staff_id'] = $userinfo['staff_id'];
                    break;
                case 3:
                    $where['detachment_id'] = $userinfo['staff_id'];
                    break;
                default;
                    $where['detachment_id'] = 1000;
                    break;
            }

            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->where($where)
                ->whereOr($whereOR ?? [])
                ->order('id desc')
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);

            foreach ($data->items() as &$v) {
                //上报人
                $v['user_staff_name'] = $this->modelUser->where('id', $v['user_staff_id'])
                    ->value('name') ?? '';
                //队长
                $v['captain_name'] = $this->modelUser->where('id', $v['captain_id'])
                    ->value('name') ?? '';
                //支队长
                $v['detachment_name'] = $this->modelUser->where('id', $v['detachment_id'])
                    ->value('name') ?? '';
                //类型
                $v['cases_type_name'] = $this->modelType->where('id', $v['cases_type_id'])
                    ->value('name') ?? '其他';
                //商户信息
                $v['shop_info'] = $this->modelShop->where('id', $v['user_shop_id'])
                    ->field('name,license,merchant_address,city,coverimage')->find() ?? [];
            }
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 问题提交
     * @return void
     */
    public function add()
    {
        $input = input('post.');
        $userinfo = $this->auth->getUserinfo();
        try {
            $this->validate($input, PatrolTaskValidate::class . '.add');
            switch ($userinfo['type']) {
                case 1://如果是执法人员
                    $input['user_staff_id'] = $userinfo['staff_id'];
                    $input['captain_id'] = $userinfo['user_id'];
                    break;
                case 2:
                    $input['captain_id'] = $userinfo['staff_id'];
                    $input['detachment_id'] = $userinfo['user_id'];
                    break;
                case 3:
                    $input['detachment_id'] = $userinfo['staff_id'];
                    break;
                default;
                    throw  new Exception('未知用户身份请先绑定身份');
            }
            $input['createtime'] = date('Y-m-d H:i:s');
            $id = $this->model->insertGetId($input);

            $this->success('保存成功', ['id' => $id]);

        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 上报支队长
     * @return void
     */
    public function appear()
    {
        $input = input('post.');
       $user = $this->auth->getUserinfo();
        try {
            if ($user['type'] != 2){
                throw  new Exception('当前身份非队长身份');
            }
            $this->validate($input, PatrolTaskValidate::class . '.appear');
            $status = $this->model->where('id', $input['id'])->value('status');
            if ($status != 1) {
                throw  new Exception('请勿重复上报');
            }
            $this->model->where('id', $input['id'])->update([
                'detachment_id' => $input['detachment_id'],
                'status' => 2
            ]);
            $this->success('保存成功', ['id' => $input['id']]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


}